package patrol.mobile.com.mobilepatrol.activity.office;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.NewsDetailsBean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.StringUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

public class HtmlWebViewActivity extends BaseActivity {
    private static final String TITLE = "TITLE";
    private static final String HTML_DATA = "HTML_DATA";
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;
    private String title;
    @BindView(R.id.webview)
    WebView webview;
    private String htmlData = "";

    public static void show(Context context, String title, String htmlData) {
        Intent intent = new Intent(context, HtmlWebViewActivity.class);
        intent.putExtra(TITLE, title);
        intent.putExtra(HTML_DATA, htmlData);
        context.startActivity(intent);
    }

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(HtmlWebViewActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        title = getIntent().getStringExtra(TITLE);
        htmlData = getIntent().getStringExtra(HTML_DATA);
        if (StringUtils.isEmpty(title)) {
            tv_center_title.setText("消息详情");
        } else {
            tv_center_title.setText(title);
        }
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_html_web_view;
    }

    @Override
    public void initData() {
        super.initData();
        initWebView();
//        webview.loadData(htmlData, "text/html;charset=utf-8", "utf-8");
        requestData();
    }

    private void requestData() {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.NEWS_ENTITY);
        sender.requestPost("id", htmlData, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                NewsDetailsBean newsDetailsBean = GsonUtil.GsonToBean(response, NewsDetailsBean.class);
                webview.loadDataWithBaseURL(null, newsDetailsBean.getContent(), "text/html", "utf-8", null);
                tvTitle.setText(newsDetailsBean.getTitle());
                tvTime.setText(newsDetailsBean.getCreateDate());
            }
        });
    }

    private void initWebView() {
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        // 设置可以支持缩放
        settings.setSupportZoom(true);
        // 设置出现缩放工具
        settings.setBuiltInZoomControls(true);
        //不显示webview缩放按钮
        settings.setDisplayZoomControls(true);
        //扩大比例的缩放
        settings.setUseWideViewPort(true);
        settings.setTextZoom(350);
//        settings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);// 默认缩放模式
    }

    @Override
    public void setListener() {

    }

}
